package mpcs.model
{
	import mpcs.ApplicationFacade;
	import mpcs.NotiConst;
	import mpcs.model.vo.GlobalVO;
	import mpcs.model.vo.UserVO;
	import mpcs.mylibs.data.SimpleCmd;
	import mpcs.mylibs.data.SimpleMsg;
	import mpcs.mylibs.events.GenericEvent;
	import mpcs.mylibs.interfaces.IConnection;
	import mpcs.mylibs.manager.ConManager;
	import mpcs.utils.TrimUtils;
	import mpcs.view.LoginMediator;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.patterns.proxy.Proxy;

	/**
	 *  
	 * @author zhangzuoqiang
	 */
	public class LoginProxy extends Proxy {
		
		public static const NAME:String = "LoginProxy";
		
		private var con:IConnection = ConManager.connection;
		private var vo:UserVO ;

		public function LoginProxy( ) {
			super( NAME );
			
			con.addEventListener("500102", rLoginHandle);
		}
		
		// 客户登录请求处理
		public function sLogin(uservo:UserVO):void{
			vo = uservo;
			trace("LoginProxy ...  sLogin ... " + uservo.toString());
			var cmd:SimpleCmd = new SimpleCmd(100102);
			cmd.writeString(uservo.getEmail());
			cmd.writeString(uservo.getPassword());
			con.send(cmd);
		}
		
		// 客户登录请求返回
		private function rLoginHandle(evt:GenericEvent):void {
			
			var msg:SimpleMsg = evt.getMsg() as SimpleMsg;
			
			if(msg.getHeadData(1) == 0) {
				trace(TrimUtils.Trim(msg.readString()));
				sendNotification(NotiConst.R_LOGIN_SUCCESS);	
				// 设置用户信息，全局变量
				GlobalVO.userVO = vo;
				// 释放资源，登陆框的事件监听
				var mediator:LoginMediator = ApplicationFacade.getInstance().retrieveMediator("LoginMediator") as LoginMediator;
				mediator.removeListeners();
				// 登陆成功，发送请求用户信息===================================
//				trace(" 登陆成功，可以请求用户信息 ... rLoginHandle ... LoginProxy ... ");
//				sendNotification(NotiConst.CAN_S_BASICINFO);
				
				// 一定要在信息成功返回时才释放该监听，不然会导致下次请求失效！！！！！！！！！！！！！！！！！！！
				con.removeEventListener("500102", rLoginHandle);
				
			}else{
				sendNotification(NotiConst.R_LOGIN_FAILED, msg.getHeadDatas());
			}
			GlobalVO.mpcsApp.loginForm.loginBtn.enabled = true;
			
//			con.removeEventListener("500102", rLoginHandle);
		}
	}
}